{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "21eaed24",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "232367fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-7.2444e-35,  5.6192e-43, -7.2444e-35],\n",
       "        [ 5.6192e-43, -7.2444e-35,  5.6192e-43],\n",
       "        [-7.2444e-35,  5.6192e-43, -7.2444e-35],\n",
       "        [ 5.6192e-43, -7.2444e-35,  5.6192e-43],\n",
       "        [-7.2444e-35,  5.6192e-43, -7.2444e-35]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.empty(5, 3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8461ae80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.9598, 0.4925, 0.0499],\n",
       "        [0.3792, 0.3685, 0.1273],\n",
       "        [0.3457, 0.1253, 0.5834],\n",
       "        [0.3673, 0.5348, 0.8892],\n",
       "        [0.0385, 0.2909, 0.6100]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5行3列随机数\n",
    "x = torch.rand(5, 3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30e964a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 0, 0],\n",
       "        [0, 0, 0],\n",
       "        [0, 0, 0],\n",
       "        [0, 0, 0],\n",
       "        [0, 0, 0]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化一个全0矩阵\n",
    "x = torch.zeros(5, 3, dtype=torch.long)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2447d08b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([5.5000, 3.0000])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接传数据\n",
    "x = torch.tensor([5.5, 3])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "98cdb58f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.],\n",
      "        [1., 1., 1.]], dtype=torch.float64)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.1406,  0.3848,  0.3973],\n",
       "        [-0.7513,  0.7815,  0.9760],\n",
       "        [ 0.0733, -0.9397,  0.1491],\n",
       "        [-1.3800, -0.3940,  0.2157],\n",
       "        [ 0.7460,  0.6930, -1.1886]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = x.new_ones(5, 3, dtype=torch.double)\n",
    "print(x)\n",
    "x = torch.randn_like(x, dtype=torch.float)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8cc747c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([5, 3])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵大小\n",
    "x.size()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b504beb",
   "metadata": {},
   "source": [
    "### 基本计算方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1f0d626b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.0508,  0.7135,  0.8340],\n",
      "        [-0.4267,  1.2928,  1.3172],\n",
      "        [ 0.6116, -0.2825,  1.0898],\n",
      "        [-1.1639,  0.1168,  0.8698],\n",
      "        [ 0.9386,  1.2401, -0.7822]])\n",
      "tensor([[ 1.0508,  0.7135,  0.8340],\n",
      "        [-0.4267,  1.2928,  1.3172],\n",
      "        [ 0.6116, -0.2825,  1.0898],\n",
      "        [-1.1639,  0.1168,  0.8698],\n",
      "        [ 0.9386,  1.2401, -0.7822]])\n"
     ]
    }
   ],
   "source": [
    "# 矩阵加法\n",
    "y = torch.rand(5, 3)\n",
    "print(x + y)\n",
    "\n",
    "print(torch.add(x, y))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d00e186",
   "metadata": {},
   "source": [
    "### 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "29fc0d5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.3848,  0.7815, -0.9397, -0.3940,  0.6930])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一列\n",
    "x[:, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "577ddc2e",
   "metadata": {},
   "source": [
    "### view操作可以改变矩阵的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9386c86e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])\n"
     ]
    }
   ],
   "source": [
    "x = torch.randn(4, 4)\n",
    "y = x.view(16)\n",
    "z = x.view(-1, 8)\n",
    "print(x.size(), y.size(), z.size())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2df7b7e4",
   "metadata": {},
   "source": [
    "### 与Numpy的协同操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c055bc9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1.], dtype=float32)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将tensor数据转为numpy的数组\n",
    "a = torch.ones(5)\n",
    "b = a.numpy()\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a1e2c924",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 1., 1., 1., 1.], dtype=torch.float64)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy数组 转为 tensor\n",
    "import numpy as np\n",
    "a = np.ones(5)\n",
    "b = torch.from_numpy(a)\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "957ecdda",
   "metadata": {},
   "source": [
    "## 常见Tensor格式\n",
    "\n",
    "#### Tensor常见的形式\n",
    "- 1. scalar\n",
    "- 2. vector\n",
    "- 3. matrix\n",
    "- 4. n-dimensional tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "684e07f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import tensor"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4772232a",
   "metadata": {},
   "source": [
    "### Scalar\n",
    "\n",
    "通常就是一个数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "daef7e39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(42.)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = tensor(42.)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c53e7165",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.dim()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b6310f8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(84.)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3a55d066",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.item()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6764dfe4",
   "metadata": {},
   "source": [
    "### Vector\n",
    "\n",
    "例如：[-5., 2., 0.], 在深度学习中通常指特征，例如词向量特征，某一维度特征等\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6fd48ca4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 1.5000, -0.5000,  3.0000])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = tensor([1.5, -0.5, 3.0])\n",
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cff9f13e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v.dim()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "41b86601",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v.size()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5516724",
   "metadata": {},
   "source": [
    "### Matrix\n",
    "\n",
    "- 一般计算的都是矩阵，通常都是多维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "82b5421b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 2.],\n",
       "        [3., 4.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = tensor([[1., 2.], [3., 4.]])\n",
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fedcbda4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 7., 10.],\n",
       "        [15., 22.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵乘法\n",
    "M.matmul(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8cf9287b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 2.])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tensor([1., 0.]).matmul(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "91e24a32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.,  4.],\n",
       "        [ 9., 16.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M * M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "080739dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 7., 10.])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tensor([1., 2.]).matmul(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77609cbd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d29100f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
